package me.jiatao.usercenter.ui.activity

import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import kotlinx.android.synthetic.main.activity_login.*
import me.jiatao.baselibrary.ext.enable
import me.jiatao.baselibrary.ext.onClick
import me.jiatao.baselibrary.ext.textString
import me.jiatao.baselibrary.ui.activity.BaseMvpActivity
import me.jiatao.baselibrary.ui.activity.BaseMvpSwipeBackActivity
import me.jiatao.provider.router.RouterPath
import me.jiatao.usercenter.R
import me.jiatao.usercenter.data.protocol.UserInfo
import me.jiatao.usercenter.injection.component.DaggerUserComponent
import me.jiatao.usercenter.mvp.presenter.LoginPresenter
import me.jiatao.usercenter.mvp.view.LoginView

@Route(path = RouterPath.UserCenter.PATH_LOGIN)
class LoginActivity : BaseMvpSwipeBackActivity<LoginView, LoginPresenter>(), LoginView, View.OnClickListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        initView()
    }

    override fun injectComponent() {
        DaggerUserComponent.builder()
            .activityComponent(mActivityComponent)
            .build()
            .inject(this)
    }


    /**
     *  初始化视图
     */
    private fun initView() {
        btn_login.enable(et_username) { isBtnEnable() }
        btn_login.enable(et_password) { isBtnEnable() }
        btn_login.onClick(this)

    }

    /**
     * 判断注册按钮是否可用
     */
    private fun isBtnEnable(): Boolean {
        return et_username.text.isNullOrEmpty().not() &&
                et_password.text.isNullOrEmpty().not()
    }

    /**
     * 点击事件
     */
    override fun onClick(view: View) {
        when (view.id) {
            R.id.btn_login -> {
                mPresenter.login(et_username.textString(), et_password.textString())
            }
        }
    }


    override fun onLoginResult(result: UserInfo) {
        showToast("登录成功")
    }
}